#include <bits/stdc++.h>

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; i++)
#define _rep(i, a, b) for (int i = (a), i##end = (b); i >= i##end; i--)
#define dl make_pair
#define dk(...) make_tuple(__VA_ARGS__)
#define ec first
#define fb second
#define de(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef __int128 i128;
typedef pair <int, int> pii;

int read() {
	int x = 0, w = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') w = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + (ch ^ 48);
		ch = getchar();
	}
	return x * w;
}

template <typename _Tp>
void print(_Tp x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) print(x / 10);
	putchar(x % 10 + 48);
}

const int MOD = 1e9 + 7;
int T;

int ans[10000005];

int m(int x) {
	int cnt[10] = {0};
	while (x) cnt[x % 10]++, x /= 10;
	int mx = 0, ps = 0;
	_rep (i, 9, 0) {
		if (cnt[i] > mx) ps = i, mx = cnt[i];
	}
	return ps;
}

int main() {
#ifndef LOCAL
	freopen("give.in", "r", stdin);
	freopen("give.out", "w", stdout);
#endif
	T = read();
	rep (i, 1, 1e7) {
		ans[i] = ans[i - 1] + m(i);
	}
	while (T --> 0) {
		int n = read();
		print(ans[n]), puts("");
	}
	return 0;
}
